package com.yuji.aacar.widgets.takephoto.app;

import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.yuji.aacar.widgets.takephoto.compress.CompressConfig;
import com.yuji.aacar.widgets.takephoto.model.CropOptions;
import com.yuji.aacar.widgets.takephoto.model.LubanOptions;
import com.yuji.aacar.widgets.takephoto.model.TakePhotoOptions;

import java.io.File;

import static com.darsh.multipleimageselect.helpers.Constants.limit;


/**
 * - 支持通过相机拍照获取图片
 * - 支持从相册选择图片
 * - 支持从文件选择图片
 * - 支持多图选择
 * - 支持批量图片裁切
 * - 支持批量图片压缩
 * - 支持对图片进行压缩
 * - 支持对图片进行裁剪
 * - 支持对裁剪及压缩参数自定义
 * - 提供自带裁剪工具(可选)
 * - 支持智能选取及裁剪异常处理
 * - 支持因拍照Activity被回收后的自动恢复
 */
public class CustomHelper {

    public static final int TAKE_PIC_TYPE_CAMERA = 0;
    public static final int TAKE_PIC_TYPE_ALBUM = 1;
    public static String AACAR_DIR = "/AaCar/temp/";
    public static String AACAR_THUMB_DIR = "/thumb/";

    private View rootView;
    int maxSize= 102400; //文件不能超过 100K
    int width= 800; //文件大小宽度
    int height= 800; //文件大小高度
    boolean showProgressBar= true; //显示压缩进度
    boolean enableRawFile =  true; //保存源文件
    private boolean rgCrop = true; //是否裁剪
    private boolean rgCompressTool = true; //是否采用自带压缩方法， false用luban的方法
    private boolean rgPickTool = false; //是否采用自带相册, false时才能调用文件夹和系统相册
    private boolean rgCorrectTool = true; //纠正拍照的照片旋转角度
    private boolean rgCropTool = false; //是否采用自带控件， 还是第三方的裁切控件
    private boolean rgCropSize = false; //true 采用宽/高比例，  false 宽 * 高 比例
    private boolean rgFrom = false; //true 从自带文件，  false 宽 * 高 比例
    private RadioGroup rgCompress,rgShowProgressBar,rgRawFile;
    private int etCropHeight = 800; //裁剪高
    private int etCropWidth = 800; //裁剪宽
    private int etLimit = 1; //选择图片裁剪个数
    private EditText etSize,etHeightPx,etWidthPx;
    public static CustomHelper of(View rootView){
        return new CustomHelper(rootView);
    }
    private CustomHelper(View rootView) {
        this.rootView = rootView;
        init();
    }
    private void init(){

    }

    public void onClick(int takePictureType, TakePhoto takePhoto) {
        File file=new File(Environment.getExternalStorageDirectory(), AACAR_DIR + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }

        Uri imageUri = Uri.fromFile(file);

        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);
        switch (takePictureType){
            case TAKE_PIC_TYPE_ALBUM: //相册
                if(etLimit>1){ //多张选择， 直接进入自带相册
                    if(rgCrop){
                        takePhoto.onPickMultipleWithCrop(limit,getCropOptions());
                    }else {
                        takePhoto.onPickMultiple(limit);
                    }
                    return;
                }
                if(rgFrom){ //从文件
                    if(rgCrop){
                        takePhoto.onPickFromDocumentsWithCrop(imageUri,getCropOptions());
                    }else {
                        takePhoto.onPickFromDocuments();
                    }
                    return;
                }else {//从相册
                    if(rgCrop){ //是否裁剪
                        takePhoto.onPickFromGalleryWithCrop(imageUri,getCropOptions());
                    }else {
                        takePhoto.onPickFromGallery();
                    }
                }
                break;
            case TAKE_PIC_TYPE_CAMERA: //相机
                if(rgCrop){
                    takePhoto.onPickFromCaptureWithCrop(imageUri,getCropOptions());
                }else {
                    takePhoto.onPickFromCapture(imageUri);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 是否选择自带相册
     * @param takePhoto
     */
    private void configTakePhotoOption(TakePhoto takePhoto){
        TakePhotoOptions.Builder builder=new TakePhotoOptions.Builder();
        if(rgPickTool){
            builder.setWithOwnGallery(true);
        }
        if(rgCorrectTool){
            builder.setCorrectImage(true);
        }
        takePhoto.setTakePhotoOptions(builder.create());

    }
    private void configCompress(TakePhoto takePhoto){

        CompressConfig config;
        if(rgCompressTool){
            config=new CompressConfig.Builder()
                    .setMaxSize(maxSize)
                    .setMaxPixel(width>=height? width:height)
                    .enableReserveRaw(enableRawFile)
                    .create();
        }else {
            LubanOptions option=new LubanOptions.Builder()
                    .setMaxHeight(height)
                    .setMaxWidth(width)
                    .setMaxSize(maxSize)
                    .create();
            config=CompressConfig.ofLuban(option);
            config.enableReserveRaw(enableRawFile);
        }
        takePhoto.onEnableCompress(config,showProgressBar);


    }
    private CropOptions getCropOptions(){
        if(!rgCrop)return null;
        CropOptions.Builder builder=new CropOptions.Builder();

        if(rgCropSize){
            builder.setAspectX(width).setAspectY(etCropHeight);
        }else {
            builder.setOutputX(width).setOutputY(etCropHeight);
        }
        builder.setWithOwnCrop(rgCropTool);
        return builder.create();
    }

}
